<!--suppress HtmlUnknownTag -->
<root xmlns="http://www.w3.org/1999/xhtml"
      xmlns:py="http://genshi.edgewall.org/"
      xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="rds.html"/>

  <?python
      from mnemoply.utils import format_date
      import os,datetime
      from base64 import b64encode
      from mnemoply.model.agreement import StudentAgreement
      from mnemoply.model.field import Group, StudentInGroup,LessonInSchedule
      from mnemoply.utils import getBarcodeImage, read_binary_file
      from mnemoply.common_knowledge import month_captions
  ?>

  <report>
    <pages py:if="ids">
      <?python
           objs = [StudentAgreement.get(int(x)) for x in ids]
       ?>

      <page py:for="agr in objs">
      <?python
                find =StudentInGroup.selectBy(studentAgreement=agr.id)
                gr = None
                less = 0
                if find.count() > 0:
                    gr_id=list(find)[0].groupID
                    gr = Group.get(gr_id)
                if not gr is None:
                    find =LessonInSchedule.selectBy(group=gr)
                    less = find.count()
                barcodes_dir = "barcodes"
                tmp_payment_type = "00"
                barcode_num = tmp_payment_type + str(agr.id).rjust(5, "0")
                barcode_file = getBarcodeImage(barcode_num, barcodes_dir)
                barcode_b64 = b64encode(read_binary_file("./" + barcode_file))
      ?>  
            <number py:content="str(agr.student.id) +'-'+str(agr.customer.id)"/>
            <customer py:content="agr.customer.title" />
            <student py:content="agr.student.fullName" />
            <birthday py:content="format_date(agr.student.birthday)" />
            <school_name py:content="agr.student.schoolName" />
            <school_form py:content="agr.student.schoolForm" />
            <phone py:content="agr.customer.phone_main"/>
            <agr_id py:content="agr.id"/>
            <group py:if="gr" py:content="gr.shortName" />
            <end_date py:if="gr" py:content="format_date(gr.endDate)" />
            <start_date py:if="gr" py:content="format_date(gr.startDate)" />
            <duration py:if="gr" py:content="gr.lessonPeriod"/>
            <plan py:if="gr" py:content="gr.initialPlan.name"/>
            <tariff py:if="gr" py:content="gr.customerTariff.title[ gr.customerTariff.title.find('/')+1:]"/>
            <comment py:if="gr" py:content="gr.comment"/>
            <duration py:if="gr" py:content="gr.lessonPeriod"/>
            <hour py:if="gr" py:content="len(gr.getLessons())*gr.lessonPeriod"/>
            <trend py:if="gr" py:content="gr.initialPlan.trend.name"/>
            <lessons py:content="less"/>
            <number_pr py:content="agr.ordinal"/>
            <barcode_num py:content="'barcode_'+str(barcode_num)" />
            <barcode>
                <filename>${barcode_num}.png</filename>
                <data py:content="barcode_b64" />
            </barcode>

       </page>
    </pages>
  </report>

</root>
